# -*- coding: utf-8 -*-

import scrapy
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from ..base_parser import BasePageParser
from ...utils import ContentProcessor, FileUtils

class FireAndLifeSafetyEcosystemParser(BasePageParser):
    """NFPA Fire and Life Safety Ecosystem页面解析器"""
    
    def can_handle(self, response):
        """判断是否能处理该响应"""
        return (
            'fire-and-life-safety-ecosystem' in response.url or
            response.meta.get('page_type') == 'nfpa_fire_life_safety'
        )
    
    def parse(self, response):
      
        """解析Fire and Life Safety Ecosystem页面"""
        soup = BeautifulSoup(response.text, 'lxml')
        
        # 创建WebPageItem
        item = self.create_webpage_item(response)
        
        item['title'] = 'What Is the NFPA Fire & Life Safety Ecosystem?'
        item['category'] = ['Home', 'About NFPA', 'NFPA Fire and Life Safety Ecosystem']
        
        # 提取正文内容
        div_list = soup.select('#maincontent > div')
        if len(div_list) > 2:
            content_elem = div_list[2]
            if content_elem:
                # 处理图文交错内容
                main_body, media_urls = ContentProcessor.process_content_with_media(
                    str(content_elem), response.url, 'nfpa'
                )
                item['main_body'] = main_body

                # 更新媒体URLs
                from .common import update_media_urls
                update_media_urls(item, media_urls)
        
        yield item
    